package com.ziyun.erp.web.modules.goods.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ziyun.erp.common.utils.GoodsMonitorUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.goodsMonitor.entity.GoodsMonitorEntity;
import com.ziyun.erp.modules.goodsMonitor.service.GoodsMonitorService;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.goods.entity.GoodsOptionEntity;
import com.ziyun.erp.modules.goods.service.GoodsOptionService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 属性选项表
 *
 * @author
 * @email
 * @date 2018-08-20 09:57:04
 */
@RestController
@RequestMapping("goods/goodsoption")
public class GoodsOptionController {
    @Autowired
    private GoodsOptionService goodsOptionService;

    @Autowired
    private GoodsMonitorService goodsMonitorService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goods:goodsoption:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsOptionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{optionId}")
    @RequiresPermissions("goods:goodsoption:info")
    public R info(@PathVariable("optionId") Long optionId){
        GoodsOptionEntity goodsOption = goodsOptionService.selectById(optionId);
        return R.ok().put("goodsOption", goodsOption);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("goods:goodsoption:save")
    public R save(@RequestBody GoodsOptionEntity goodsOption){
        goodsOption.setCreateName(UserUtils.getCurentUser().getUsername());
        goodsOptionService.insert(goodsOption);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("goods:goodsoption:update")
    public R update(@RequestBody GoodsOptionEntity goodsOption){
        ValidatorUtils.validateEntity(goodsOption);
        GoodsOptionEntity oldGoodsOption = goodsOptionService.selectById(goodsOption.getOptionId());
        GoodsMonitorEntity goodsMonitorEntity = GoodsMonitorUtils.packingData(oldGoodsOption, goodsOption, UserUtils.getCurentUser().getUserId());
        if(goodsMonitorEntity.getSdbId() != null){
            goodsMonitorService.insert(goodsMonitorEntity);
        }
        goodsOptionService.updateValues(goodsOption);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goods:goodsoption:delete")
    public R delete(@RequestBody Long[] optionIds){
        goodsOptionService.deleteBatchIds(Arrays.asList(optionIds));
        return R.ok();
    }

    /**
     * 获得规格属性值
     */
    @RequestMapping("/getValues/{attributeId}")
    public R getValues(@PathVariable("attributeId") Long attributeId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("attribute_id",attributeId);
        map.put("disabled",0);
        map.put("seller_id",0);
        List<GoodsOptionEntity> goodsOptionList = goodsOptionService.selectByMap(map);
        return R.ok().put("goodsOptionList",goodsOptionList);
    }

    /**
     * 信息
     */
    @RequestMapping("/flagDelete/{optionId}")
    public R flagDelete(@PathVariable("optionId") Long optionId){
        goodsOptionService.flagDeleteByOptionId(optionId);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/updateValue")
    public R updateValue(@RequestBody GoodsOptionEntity goodsOption){
        ValidatorUtils.validateEntity(goodsOption);
        goodsOptionService.updateValues(goodsOption);
        return R.ok();
    }
}
